using System;
using System.Collections.Generic;
using System.Text;

namespace Xivo.Shared
{
  public class ChannelList
  {
    ChannelInfo[] items = new ChannelInfo[256];
    public SortedList<int, ChannelInfo> AllChannels = new SortedList<int, ChannelInfo>();
    public SortedList<int, ChannelInfo> Favorites = new SortedList<int, ChannelInfo>();

    /// <summary>
    /// get a channel by number
    /// </summary>
    /// <param name="index">channel number to return</param>
    /// <returns>channel info</returns>
    public ChannelInfo Channel(int index)
    {
      if (index > items.Length - 1 || index < 0)
        return null;

      if (items[index] == null)
      {
        items[index] = new ChannelInfo();
        items[index].channel = index;
      }
      return items[index];
    }

    /// <summary>
    /// returns item index in the list
    /// this is not the channel number, but rather the n'th item in the list
    /// </summary>
    /// <param name="index">item to return</param>
    /// <returns>item pointed to by index</returns>
    public ChannelInfo ChannelAt(int index)
    {
      if (index > AllChannels.Count - 1 || index < 0)
        return null;
      return AllChannels.Values[index];
    }

    /// <summary>
    /// favorite pointed to by index
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public ChannelInfo FavoriteAt(int index)
    {
      if (index > Favorites.Count - 1 || index < 0)
        return null;
      return Favorites.Values[index];
    }

    public int Count
    {
      get
      {
        return items.Length;
      }
    }

    public void Save(SortedList<int, ChannelInfo> list, string filename)
    {
      System.IO.TextWriter outf = new System.IO.StreamWriter(filename, false);
      foreach (ChannelInfo info in list.Values)
      {
        if (info.genre != "-")
        {
          outf.WriteLine(info.channelStr + '\t' +
            info.genre + '\t' +
            info.station + '\t' +
            info.longArtist + '\t' +
            info.longTitle);
        }
      }
      outf.Close();
    }

    public void LoadList(SortedList<int, ChannelInfo> list, string filename)
    {
      if (System.IO.File.Exists(filename))
      {
        System.IO.TextReader inf = new System.IO.StreamReader(filename);
        string buf = inf.ReadLine();
        int channelNum;
        ChannelInfo info;

        while (buf != null)
        {
          string[] parts = buf.Split('\t');
          if (int.TryParse(parts[0], out channelNum) && parts.Length >= 5)
          {
            info = this.Channel(channelNum);
            info.channel = (byte)channelNum;
            info.genre = parts[1];
            info.station = parts[2];
            //info.artist = parts[3];
            //info.title = parts[4];
            if (list.ContainsKey(info.channel) == false)
              list.Add(info.channel, info);
          }
          buf = inf.ReadLine();
        }
        inf.Close();
      }
    }

    public void UpdateChannelInfo(ChannelInfo NewInfo)
    {
      if (NewInfo != null)
      {
        ChannelInfo i = Channel(NewInfo.channel);
        if (NewInfo != null && NewInfo.valid)
        {
          i.station = NewInfo.station;
          i.genre = NewInfo.genre;
          i.longArtist = NewInfo.longArtist;
          i.longTitle = NewInfo.longTitle;
        }
        if (!AllChannels.ContainsKey(i.channel))
          AllChannels.Add(i.channel, i);
      }
    }
  }
}
